import Cookies from 'js-cookie'
import Config from '@/settings'

const TokenKey = Config.TokenKey

export function getToken() {
    return Cookies.get(TokenKey)
}

export function setUserInfo(userInfo) {
    let s = JSON.stringify(userInfo);
    console.log(s);
    return Cookies.set(Config.loginInfo, s);
}

export function setCustomerType(code) {
    return Cookies.set(Config.customerTypeCode, code);
}

export function getCustomerType() {
    let code = Cookies.get(Config.customerTypeCode);
    if (!code) {
        console.log("未找到客户编码");
        return null;
    }
    return code;
}

export function getUserInfo() {
    let newVar = Cookies.get(Config.loginInfo);
    if (!newVar) {
        console.log("未找到用户信息");
        return null;
    }
    return JSON.parse(newVar);
}

export function setToken(token, rememberMe) {
    if (rememberMe) {
        return Cookies.set(TokenKey, token, {expires: Config.tokenCookieExpires})
    } else return Cookies.set(TokenKey, token)
}

export function removeToken() {
    Cookies.remove(Config.loginInfo);
    return Cookies.remove(TokenKey)
}
